#coding:utf-8
from __future__ import division
import math

from lovewith.settings import PAGE_SIZE
from lovewith.share.models import AttachGoods
from lovewith.utils.mtcache import MtCache


class AttachFlagApi:
    def __init__(self):
        pass

    #获取全部淘客数据
    @staticmethod
    def get_by_tag(tag='all', page_no=1):
        if not tag:
            tag = 'all'

        goods_data = MtCache('goods_tag').get(tag=tag, page=page_no)

        if not goods_data:
            #记录总数
            if tag != 'all':
                total = AttachGoods.objects.filter(tagname__icontains=tag).count()
            else:
                total = AttachGoods.objects.all().count()

            #总页数
            total_page = int(math.ceil(total / PAGE_SIZE))
            total_page = 1 and total_page or 1

            start = (page_no - 1) * PAGE_SIZE
            last = page_no * PAGE_SIZE

            if last > total:
                last = total

            goods_filter_data = []
            if tag != 'all':
                tk_data = AttachGoods.objects.filter(tagname__icontains=tag).order_by('-id')[start: last]
            else:
                tk_data = AttachGoods.objects.all().order_by('-id')[start: last]

            image_ids = []
            for tk in tk_data:
                if tk.post_image.id not in image_ids:
                    image_ids.append(tk.post_image.id)
                    goods_filter_data.append({
                        'share_id': tk.post_image.share_post.id,
                        'cover_id': tk.post_image.id,
                        'cover': tk.post_image.path,
                        'title': tk.product_name,
                        'url': tk.product_url,
                        'price': tk.product_price,
                        'position': tk.flag_position
                    })

            goods_data = {'goods': goods_filter_data, 'total': total, 'current_page': page_no, 'total_page': total_page}

            MtCache('goods_tag').set(goods_data, tag=tag, page=page_no)
        return goods_data

    #获取分类下所有商品
    @staticmethod
    def get_by_cate(cate_id, page_no=1):
        goods_data = MtCache('goods_cate').get(cate_id=cate_id, page=page_no)

        if not goods_data:
            #记录总数
            total = AttachGoods.objects.all().count()

            #总页数
            total_page = int(math.ceil(total / PAGE_SIZE))
            total_page = 1 and total_page or 1

            start = (page_no - 1) * PAGE_SIZE
            last = page_no * PAGE_SIZE

            if last > total:
                last = total

            goods_filter_data = []
            tk_data = AttachGoods.objects.filter(cate_id=int(cate_id)).order_by('-id')[start: last]

            image_ids = []
            for tk in tk_data:
                if tk.post_image.id not in image_ids:
                    image_ids.append(tk.post_image.id)
                    goods_filter_data.append({
                        'share_id': tk.post_image.share_post.id,
                        'cover_id': tk.post_image.id,
                        'cover': tk.post_image.path,
                        'title': tk.product_name,
                        'url': tk.product_url,
                        'price': tk.product_price,
                        'position': tk.flag_position
                    })

            goods_data = {'goods': goods_filter_data, 'total': total, 'current_page': page_no, 'total_page': total_page}

            MtCache('goods_cate').set(goods_data, cate_id=cate_id, page=page_no)
        return goods_data

    #获取图片的淘客标记
    @staticmethod
    def get_goods_by_image(image_id):
        result = []
        tk_data = AttachGoods.objects.filter(post_image_id=image_id).order_by('id')
        for tk in tk_data:
            result.append({
                'image_id': tk.post_image.id,
                'title': tk.product_name,
                'url': tk.product_url,
                'price': tk.product_price,
                'position': tk.flag_position
            })

        return result

    #淘客标记总数
    @staticmethod
    def get_goods_count():
        tk_total = MtCache('site_tk_total').get()
        if not tk_total:
            tk_total = AttachGoods.objects.all().count()
            MtCache('site_tk_total').set(tk_total)

        return tk_total

    #添加淘客标记
    @staticmethod
    def add_goods(image_data, tagname, position, title, url, price, user):
        new_tk = AttachGoods(
            user=user,
            cate=image_data.share_post.cate,
            post_image=image_data,
            tagname=tagname,
            product_name=title,
            product_url=url,
            product_price=price,
            flag_position=position
        )
        new_tk.save()
        return True

    #删除
    @staticmethod
    def delete_by_image(image_id):
        tk_flag = AttachGoods.objects.filter(post_image_id=image_id)
        tk_flag.delete()